package com.cheyunkeji.er.activity.auction;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.auction.CooperatorBaseInfo;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

public class AddCooperatorActivity extends BaseActivity  {

    private static final String TAG = AddCooperatorActivity.class.getSimpleName() ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.et_cooperator_code)
    EditText etCooperatorCode;
    @BindView(R.id.btn_next_step)
    Button btnNextStep;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_add_cooperator);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        vTopbar.setLeftBack();
        vTopbar.setTitle("添加");
        vTopbar.setRight("查看记录", -1, this);
        btnNextStep.setOnClickListener(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_next_step:

                if (TextUtils.isEmpty(etCooperatorCode.getText().toString())) {

                    SToast.show("请输入合作商代码！");
                    return;
                }

                searchCooperator(etCooperatorCode.getText().toString());

                break;

            case R.id.vRight:
                startActivity(new Intent(AddCooperatorActivity.this, ApplyRecordActivity.class));
                break ;

            default:
                break;
        }
    }


    private void searchCooperator(String cooperatorCode) {

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
        params.put("keywords", cooperatorCode);

        ApiClient.postForm(HttpConstants.SEARCH_CHANNEL, params, new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {
                // 网络异常
                if (e instanceof UnknownHostException || e instanceof SocketException) {
                    SToast.show(R.string.tip_network_error, Gravity.CENTER);
                    return;
                }
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject obj = new JSONObject(response);

                    if(TextUtils.equals(obj.optString("code"),"0")){
                        SToast.show(obj.optString("msg"));
                        return ;
                    }

                    if(null != obj && null != obj.getJSONObject("data")){

                        CooperatorBaseInfo baseInfo = new Gson().fromJson(obj.getString("data"),CooperatorBaseInfo.class);

                        if( null != baseInfo){

                            etCooperatorCode.getText().clear();
                            Intent intent = new Intent(AddCooperatorActivity.this,SubmitInfoActivity.class);
                            intent.putExtra("COOPERATOR_INFO",baseInfo);
                            startActivity(intent);

                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onBefore(Request request, int id) {
                showDialog();
            }

            @Override
            public void onAfter(int id) {
                dismissDialog();
            }

        });

    }

}
